<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="药品编号 " :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'mid', validatorRules.mid]" placeholder="请输入药品编号 "></a-input>
        </a-form-item>
        <a-form-item label="药品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'mname', validatorRules.mname]" placeholder="请输入药品名称"></a-input>
        </a-form-item>
        <a-form-item label="药品建成" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'mshortname', validatorRules.mshortname]" placeholder="请输入药品建成"></a-input>
        </a-form-item>
        <a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'mnors', validatorRules.mnors]" placeholder="请输入规格"></a-input>
        </a-form-item>
        <a-form-item label="药品产地" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'marea', validatorRules.marea]" placeholder="请输入药品产地"></a-input>
        </a-form-item>
        <a-form-item label="批号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'mlot', validatorRules.mlot]" placeholder="请输入批号"></a-input>
        </a-form-item>
        <a-form-item label="进价" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'minpri', validatorRules.minpri]" placeholder="请输入进价" :step="0.1" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="批发价" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'mwpri', validatorRules.mwpri]" placeholder="请输入批发价" :step="0.1" style="width: 100%"></a-input-number>
        </a-form-item>
        <a-form-item label="零售价" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'mpri', validatorRules.mpri]" placeholder="请输入零售价" :step="0.1" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'mcon', validatorRules.mcon]" placeholder="请输入供应商"></a-input>
        </a-form-item>
        <a-form-item label="类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'mcate', validatorRules.mcate]" placeholder="请输入类别"></a-input>
        </a-form-item>
        <a-form-item label="有效期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择有效期" v-decorator="[ 'musetime', validatorRules.musetime]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'memo', validatorRules.memo]" placeholder="请输入备注"></a-input>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import JDate from '@/components/jeecg/JDate'

  export default {
    name: "MedicineModal",
    components: {
      JDate,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules:{
        mid:{rules: [{ required: true, message: '请输入药品编号 !' }]},
        mname:{rules: [{ required: true, message: '请输入药品名称!' }]},
        mshortname:{},
        mnors:{rules: [{ required: true, message: '请输入规格!' }]},
        marea:{rules: [{ required: true, message: '请输入药品产地!' }]},
        mlot:{rules: [{ required: true, message: '请输入批号!' }]},
        minpri:{rules: [{ required: true, message: '请输入进价!' }]},
        mwpri:{rules: [{ required: true, message: '请输入批发价!' }]},
        mpri:{rules: [{ required: true, message: '请输入零售价!' }]},
        mcon:{rules: [{ required: true, message: '请输入供应商!' }]},
        mcate:{rules: [{ required: true, message: '请输入类别!' }]},
        musetime:{rules: [{ required: true, message: '请输入有效期!' }]},
        memo:{},
        },
        url: {
          add: "/medicine/medicine/add",
          edit: "/medicine/medicine/edit",
        }

      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'mid','mname','mshortname','mnors','marea','mlot','minpri','mwpri','mpri','mcon','mcate','musetime','memo'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'mid','mname','mshortname','mnors','marea','mlot','minpri','mwpri','mpri','mcon','mcate','musetime','memo'))
      },


    }
  }
</script>